//
//  XMAlbumJigsawAvatarController.m
//  XiaoMiao
//
//  Created by kinglian_iOS_000 on 2018/2/24.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMAlbumJigsawAvatarController.h"
#import "XMAlbumSubModel.h"
#import "XMAlbumSubCell.h"
#import "XMAlbumSubHeadView.h"
#import "XMAlbumSubsubModel.h"
#import "XMAlbumJigsawView.h"
#import "AlbumImageModel.h"
#import "XMJigsawController.h"
#import "XMShowRemarkView.h"


@interface XMAlbumJigsawAvatarController () <UITableViewDelegate, UITableViewDataSource, XMAlbumSubCellDelegate, KLTableViewPlaceHolderProtocol, XMAlbumJigsawViewDelegate>
/**items*/
@property(nonatomic,strong) NSMutableArray <XMAlbumSubModel *>*items;
/** <#属性#> */
@property (nonatomic, strong) UITableView *tableView;
/** <#Description#> */
@property (nonatomic, strong) XMAlbumJigsawView *albumJigsawView;

/** <#Description#> */
@property (nonatomic, strong) NSMutableArray <AlbumImageModel *>*albumJigsawArrayM;
@end

@implementation XMAlbumJigsawAvatarController
static NSString * const Id = @"Album";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"拼图";
    
    [self setupUI];
    
    [self loadAlbumDataRequest];
}

#pragma mark - UI
- (void)setupUI
{
    //
    [self.view addSubview:self.albumJigsawView];
    [self.albumJigsawView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.equalTo(@160);
    }];
    
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.equalTo(self.albumJigsawView.mas_top);
    }];
    

    
    
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.items.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items[section].photographUserAngleViews.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMAlbumSubCell *cell = [tableView dequeueReusableCellWithIdentifier:Id];
    
    cell.delegate = self;
    cell.albumType = XMAlbumSubTypePhotos;
//    cell.isUpdataOperate = NO;
    cell.albumSubCellOperate = XMAlbumSubCellOperateJigsaw;
    cell.item = self.items[indexPath.section].photographUserAngleViews[indexPath.row];
    
    MJWeakSelf
    [cell setRemarkBlock:^{
        NSMutableArray *listArr =  weakSelf.items[indexPath.section].photographUserAngleViews;
        XMAlbumSubsubModel * subModel = listArr[indexPath.row];
        
        [XMShowRemarkView showRemarkViewWithRemarkModel:subModel.partyExtrView2];
        
        
    }];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.items[indexPath.section].photographUserAngleViews[indexPath.row].photoCellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [XMAlbumSubHeadView viewHeigt];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    XMAlbumSubHeadView *headView = [XMAlbumSubHeadView app_viewFromXib];
    headView.title = self.items[section].name;
    
    return headView;
}

#pragma mark - XMAlbumSubCellDelegate
- (void)albumSubCell:(XMAlbumSubCell *)cell didCellFold:(BOOL)isFload item:(XMAlbumSubsubModel *)item
{
    item.isFoldAlubm = isFload;
    
    [self.tableView reloadData];
}

- (void)albumSubCell:(XMAlbumSubCell *)albumSubCell didSeletedAlbumImageModel:(AlbumImageModel *)model
{

    
    if (model.isSeleted) {
        if (self.albumJigsawArrayM.count >= 12) {
            model.isSeleted = NO;
            [self.tableView reloadData];
            [KLProgressHUD showPromptHUDWithMessage:@"最多添加12张照片"];
            return;
        }else{
            [self.albumJigsawArrayM addObject:model];
        }
       
    }else {
        [self.albumJigsawArrayM removeObject:model];
    }
    
    self.albumJigsawView.albumImageModelArrayM = self.albumJigsawArrayM;
}

#pragma mark - KLTableViewPlaceHolderProtocol
- (UIView *)makePlaceHolderView
{
    return [KLNoValueView noValueView];
}

#pragma mark - XMAlbumJigsawViewDelegate
- (void)albumJigsawView:(XMAlbumJigsawView *)albumJigsawView didDeletedAlbumImageModel:(AlbumImageModel *)model
{
    for (XMAlbumSubModel *subModel in self.items) {
        for (XMAlbumSubsubModel *subsubModel in subModel.photographUserAngleViews) {
            for (AlbumImageModel *albumImageModel in subsubModel.photographUserAngleRefViews) {
                if (albumImageModel == model || albumImageModel.imageUrl == model.imageUrl) {
                    model.isSeleted = NO;
                    break;
                }
            }
        }
    }
    [self.tableView reloadData];
}

- (void)albumJigsawView:(XMAlbumJigsawView *)albumJigsawView startJigsawAlbumImageModelArrayM:(NSMutableArray <AlbumImageModel *>*)albumImageModelArrayM
{
    
    if (self.albumJigsawArrayM.count != 2&& self.albumJigsawArrayM.count != 4&& self.albumJigsawArrayM.count != 6&& self.albumJigsawArrayM.count != 10&& self.albumJigsawArrayM.count != 12) {
        [KLProgressHUD showErrorHUDWithMessage:@"图片数量只能是2、4、6、10、12张"];
        return;
    }
    //下载图片
    NSMutableArray *arrayM = [NSMutableArray array];
    for (AlbumImageModel *imageModel in albumImageModelArrayM) {
        
        NSString *imageUrl = imageModel.imageUrl;
        //获取缓存的图片
        UIImage *image = [[SDImageCache sharedImageCache] imageFromCacheForKey:imageUrl];
        if (image == nil) {
            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderHighPriority progress:nil completed:nil];
        }else{
            [arrayM addObject:image];
        }
    }
    
    // TODO:llq 需要设计参数传入
    XMJigsawController *jigsaw = [[XMJigsawController alloc] init];
    jigsaw.imagesArray = arrayM;
    [self.navigationController pushViewController:jigsaw animated:YES];
}

#pragma mark - Request
// 图库数据数据请求
- (void)loadAlbumDataRequest
{
    KLProgressHUD *progressHUD = [KLProgressHUD showWaitingHUDWithSuperView:self.view message:kLoadingMsg];
    
    WEAKSELF
    [AppNetwork album_getLibraryWithPartyId:self.partyId completion:^(BOOL success, id response, NSString *errMsg) {
        progressHUD.hidden = YES;
        
        if (success) {
            NSArray * list = [NSArray modelArrayWithClass:[XMAlbumSubModel class] json:response[@"photographUserAngleMapViews"]];
            //添加查看备注和删除
            
                for (XMAlbumSubModel * subModel in list) {
                    for (XMAlbumSubsubModel * subSubModel in subModel.photographUserAngleViews) {
                        AlbumImageModel * remarkModel = [[AlbumImageModel alloc]init];
                        remarkModel.titleStr = @"查看备注";
                        [subSubModel.photographUserAngleRefViews addObject:remarkModel];
                    }
                    
                }
            
            weakSelf.items = [NSMutableArray arrayWithArray:list];
            
            [weakSelf.tableView app_reloadData];
        }else {
            [KLProgressHUD showErrorHUDWithMessage:errMsg];
        }
    }];
}

#pragma mark - lazy
- (NSMutableArray *)items
{
    if (_items == nil) {
        _items = [NSMutableArray array];
    }
    return _items;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.sectionIndexColor = HEXPColor(0x2c2e30, 1.0);
        
        [tableView registerNib:[UINib nibWithNibName:NSStringFromClass(XMAlbumSubCell.class) bundle:nil] forCellReuseIdentifier:Id];
        tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0);
        
        _tableView = tableView;
    }
    return _tableView;
}

- (XMAlbumJigsawView *)albumJigsawView {
    if (!_albumJigsawView) {
        _albumJigsawView = [XMAlbumJigsawView app_viewFromXib];
        _albumJigsawView.deleagte = self;
    }
    return _albumJigsawView;
}

- (NSMutableArray<AlbumImageModel *> *)albumJigsawArrayM {
    if (!_albumJigsawArrayM) {
        _albumJigsawArrayM = [NSMutableArray array];
    }
    return _albumJigsawArrayM;
}
@end
